package v1c08;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @author gjunming@gmail.com
 * @version Aug 6, 2009 3:13:24 PM
 */
public class ButtonEvent {

	private static final int FWIDTH = 300;
	private static final int FHEIGHT = 200;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ButtonFrame buttonFrame = new ButtonFrame();
		buttonFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		buttonFrame.setTitle("Button Event");
		buttonFrame.setSize(FWIDTH, FHEIGHT);
		buttonFrame.setVisible(true);
	}
}

class ButtonFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private final JPanel buttonPanel;

	public ButtonFrame() {
		buttonPanel = new JPanel();

		add(buttonPanel);

		JButton yellowButton = new JButton("Yellow");
		JButton blueButton = new JButton("Blue");
		JButton redButton = new JButton("Red");

		buttonPanel.add(yellowButton);
		buttonPanel.add(blueButton);
		buttonPanel.add(redButton);

		// achive action
		createButton(redButton, Color.RED);
		createButton(blueButton, Color.BLUE);
		createButton(yellowButton, Color.YELLOW);
	}

	public void createButton(JButton button, final Color c) {
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				buttonPanel.setBackground(c);
			}
		});
	}
}
